<script setup name="WarehouseSelect" lang="ts">
import {listWarehouse, WarehouseVO} from "@/views/wms/warehouse/api";

// 声明外部传入的数据
const props = defineProps({
  whId: {type: Number, default: null},
  params: {type: Object, default: undefined},
  isArea: {type: Boolean, default: false},
});
const pd = reactive({
  warehouseList: <WarehouseVO[]>([]),
  queryParams: <WarehouseVO>{
    parentId: undefined,
    name: undefined
  }
});

const {proxy} = getCurrentInstance() as ComponentInternalInstance


/** 查询测试树列表 */
async function getList() {
  pd.queryParams = {...pd.queryParams, ...props.params};
  const res = await listWarehouse(pd.queryParams);
  if (props.isArea){
    res.data = res.data.filter(item => {return item.parentId != 0})
  }
  const data = proxy?.handleTree<WarehouseVO>(res.data, "id", "parentId");
  if (data) {
    pd.warehouseList = data;
    console.log(pd.warehouseList)
  }
}
// 定义需要回传的数据
const emits = defineEmits(["update:whId"]);

const whId = computed({
  get() {
    return props.whId
  },
  set(val) {
    emits("update:whId", val)
  }
})

onMounted(() => {
  getList();
});
</script>

<template>
<!--  <el-select v-model="whId" clearable>
    <el-option v-for="item in pd.warehouseList" :key="item.id" :label="item.name" :value="Number(item.id)"/>
  </el-select>-->
  <el-tree-select
    v-model="whId"
    :data="pd.warehouseList"
    :render-after-expand="false"
    :expand-on-click-node="false"
    check-strictly
    :props="{
      children: 'children',
      label: 'name',
      value: 'id'
    }"
  />
</template>
